import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import { createPinia } from 'pinia';
import { Core } from './core';
import { useWindowStore } from './store/window';
import { EventCode } from '../events';
import metadata from '../metadata.json';
import 'animate.css/animate.min.css';
import 'element-plus/theme-chalk/dark/css-vars.css';
import './assets/style/index.scss';
import './assets/style/dark/index.css';
import './assets/style/font/HarmonyOS_Sans_SC/index.css';
import { isUndefined } from './core/is';
import { useMessage } from './hooks/message';
import { ElLoading } from 'element-plus';
import { useShortcutKey } from './hooks/shortcut-key';

const app = createApp(App);
const pinia = createPinia();

app.use(router);
app.use(pinia);
app.directive('loading', ElLoading.directive);

// 网页版环境变量模拟
if (typeof window !== 'undefined' && typeof (window as any).GLOBAL_IPC === 'undefined') {
  (window as any).GLOBAL_DB = {
    // 简单的内存数据库模拟
    books: new Map(),
    authors: new Map(),
    categories: new Map(),
    bookshelves: new Map(),
  };
  (window as any).GLOBAL_IPC = {
    on: (event: string, callback: Function) => console.log(`IPC.on: ${event}`),
    once: (event: string, callback: Function) => console.log(`IPC.once: ${event}`),
    send: (event: string, ...args: any[]) => console.log(`IPC.send: ${event}`, args),
    removeAllListeners: (event: string) => console.log(`IPC.removeAllListeners: ${event}`),
  };
  (window as any).GLOBAL_LOG = {
    info: (message: string) => console.log(`[INFO] ${message}`),
    error: (message: string) => console.error(`[ERROR] ${message}`),
    warn: (message: string) => console.warn(`[WARN] ${message}`),
    debug: (message: string) => console.debug(`[DEBUG] ${message}`),
  };
  (window as any).GLOBAL_PLUGINS = {
    list: [],
    get: (id: string) => null,
    install: (plugin: any) => console.log('Plugin install:', plugin),
    uninstall: (id: string) => console.log('Plugin uninstall:', id),
  };
  
  // 添加 METADATA 全局变量
  (window as any).METADATA = metadata;
  
  // 模拟 process 对象
  if (typeof (window as any).process === 'undefined') {
    (window as any).process = {
      platform: 'web',
      env: {
        NODE_ENV: 'development'
      }
    };
  }
}

// 确保全局变量存在
declare global {
  var GLOBAL_LOG: any;
  var GLOBAL_IPC: any;
  var GLOBAL_DB: any;
  var GLOBAL_PLUGINS: any;
  var METADATA: any;
}

const initHeaderColor = () => {
  console.log('Header color initialization (web version)');
};

// 简化的初始化过程
app.mount('#app').$nextTick().then(() => {
  // 简化的 Core.init 调用
  Promise.resolve().then(() => {
    console.log(`ReadCat started - tag: ${metadata.name}, version: ${metadata.version}`);
    
    const win = useWindowStore();
    const initedEvents = win.events.get('inited');
    if (initedEvents) {
      Object.keys(initedEvents).forEach(key => {
        initedEvents[key] && initedEvents[key]();
      });
    }
    
    // 移除加载动画
    if (typeof window !== 'undefined') {
      window.postMessage({ payload: 'removeLoading' }, '*');
    }
    
    // 初始化快捷键
    try {
      useShortcutKey();
    } catch (e) {
      console.warn('Shortcut key initialization failed:', e);
    }
    
    console.log('ReadCat application initialized successfully!');
  }).catch((error) => {
    console.error('ReadCat initialization error:', error);
    const message = useMessage();
    message.error(`初始化错误: ${error.message}`);
  });
});